<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\Visit;
use Carbon\Carbon;

class LogRequestParams
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //当前时间
        $nowTime = Carbon::now()->toDateTimeString();
        $adminId = Request()->get('wechat_admin_id', 0);
        if($adminId <= 0) {
            $userInfo = session()->get('login_user');
            $adminId = $userInfo['id'];
        }

        $visitData = [
            'admin_id' => $adminId,
            'ip' => $request->ip(),
            'url' => $request->url(),
            'http_user_agent' => $request->userAgent(),
            'extra_data' => json_encode([
                'http_referer' => isset($_SERVER['HTTP_REFERER']) ? isset($_SERVER['HTTP_REFERER']) : '',
                'route' => '/'.$request->path(),
            ], JSON_UNESCAPED_UNICODE),
            'request' => json_encode($request->all(), JSON_UNESCAPED_UNICODE),
            'timestamp' => $nowTime
        ];
        // Visit::insert($visitData);

        return $next($request);
    }
}
